﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Configuration;

public partial class SaleWrComment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["group"] == null)
        {
            Response.Redirect("../Login.aspx");
        }
        else if ((int)Session["group"] != 1)
        {
            Response.Redirect("../Login.aspx");
        }
        SqlConnection sqlcon = null;
        SqlCommand sqlcmd = null;
        string cmd = "";
        sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["testLottery"].ConnectionString);
        sqlcon.Open();
        cmd = "select distinct Member.id,Member.name from Node,AreaManager,Member where (Node.area=AreaManager.area AND AreaManager.member=Member.id AND Node.member='"+Session["uid"]+"') OR (Member.[group]=3);";
        sqlcmd = new SqlCommand(cmd, sqlcon);
        SqlDataAdapter da = new SqlDataAdapter(cmd, sqlcon);
        DataSet ds = new DataSet();
        da.Fill(ds, "anyway");
        if (!IsPostBack)
        {
            CommTo.DataSource = ds.Tables["anyway"].DefaultView;
            CommTo.DataTextField = "name";
            CommTo.DataValueField = "id";
            CommTo.DataBind();
        }
        sqlcon.Close();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        int cid = 0;
        bool i = false;
        string CommId = "";
        //read text
        string to = CommTo.Text;
        string title = CommTitle.Text;
        string text = CommText.Text;
        DateTime dt = DateTime.Now;
        if (title == "")
        {
            titleerror.Text = "请输入标题！";
            texterror.Text = "";
        }
        else if (text == "")
        {
            titleerror.Text = "";
            texterror.Text = "请输入正文！";
        }
        else
        {
            SqlConnection sqlcon = null;
            SqlCommand sqlcmd = null;
            SqlCommand sqlcmd2 = null;
            SqlCommand sqlcmd4 = null;
            SqlCommand sqlcmd5 = null;
            SqlDataReader read = null;
            string cmd = "";
            try
            {
                sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["testLottery"].ConnectionString);
                sqlcon.Open();
                cmd = "insert into Notice(fmember,tmember,title,[content],time) values ('" + Session["uid"] + "','" + to + "','" + title + "','" + text + "','" + dt + "'   )";
                sqlcmd = new SqlCommand(cmd, sqlcon);
                //insert
                sqlcmd.ExecuteNonQuery();
                titleerror.Text = "";
                texterror.Text = "发布成功！";
                //CommTo.Text = "";
                //get annoid
                cmd = "select max(id) from Notice";
                sqlcmd2 = new SqlCommand(cmd, sqlcon);
                read = sqlcmd2.ExecuteReader();
                i = read.Read();
                cid = Int16.Parse(read.GetValue(0).ToString());
                read.Close();
                if (i)
                {
                    cmd = "select notice from Member where id='" + to + "'";
                    sqlcmd4 = new SqlCommand(cmd, sqlcon);
                    read = sqlcmd4.ExecuteReader();
                    read.Read();
                    CommId = read.GetValue(0).ToString();
                    read.Close();
                    CommId = CommId + cid + ";";
                    cmd = "update member set notice='" + CommId + "' where id='" + to + "'";
                    sqlcmd5 = new SqlCommand(cmd, sqlcon);
                    sqlcmd5.ExecuteNonQuery();
                }
                else
                {
                    ;
                }
            }
            catch (SqlException sqle)
            {
                ;
            }
            finally
            {
                sqlcon.Close();
            }
        }
    }
}